package alp.starcode.sanping.dataapply.controller;

import alp.starcode.common.base.annotation.operate.Add;
import alp.starcode.common.base.annotation.operate.Delete;
import alp.starcode.common.base.annotation.operate.Update;
import alp.starcode.common.mybatis.page.Pagination;
import alp.starcode.common.mybatis.sort.Sorter;
import alp.starcode.common.web.result.Result;
import alp.starcode.sanping.dataapply.model.dto.MapDataDTO;
import alp.starcode.sanping.dataapply.model.vo.MapDataVO;
import alp.starcode.sanping.dataapply.service.MapDataService;
import alp.starcode.sanping.dict.vo.DictDataVO;
import alp.starcode.sanping.framework.base.BaseController;
import alp.starcode.sanping.framework.database.mariadb.mybatis.entity.MapData;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * TODO
 *
 * @author wujie
 * @date 2024/3/14 17:32
 */
@Api(tags = "地图数据")
@RestController
@RequestMapping("mapData")
@Validated
public class MapDataController extends BaseController {

    @Resource
    private MapDataService mapDataService;

    @GetMapping("pageList.do")
    @ApiOperation("地图数据表-分页查询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchKeyword", value = "搜索关键字", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "minRatio", value = "最小比例", dataType = "Double", paramType = "query", required = false),
            @ApiImplicitParam(name = "maxRatio", value = "最大比例", dataType = "Double", paramType = "query", required = false),
    })
    @ApiOperationSupport(order = 10)
    public Result<Pagination<MapDataVO>> pageList(Pagination<MapData> pagination, Double minRatio, Double maxRatio, String searchKeyword, Sorter sorters) {
        return Result.success(mapDataService.pageList(pagination, minRatio, maxRatio, searchKeyword, sorters));
    }

    @GetMapping("getTypeDate.do")
    @ApiOperation("地图数据表-根据省查询产业数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "省份(山东省)", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "cityType", value = "城市类别(县级城市/副省级城市)", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "industryType", value = "产业类别", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "minRatio", value = "最小比例", dataType = "Double", paramType = "query", required = false),
            @ApiImplicitParam(name = "maxRatio", value = "最大比例", dataType = "Double", paramType = "query", required = false),
    })
    @ApiOperationSupport(order = 10)
    public Result<JSONArray> getTypeDate(String province, String cityType, String industryType, Double minRatio, Double maxRatio, Sorter sorters) {
        return Result.success(mapDataService.getTypeDate(province, cityType, industryType, minRatio, maxRatio, sorters));
    }

    @GetMapping("getById.do")
    @ApiOperation("地图数据表-根据ID查询信息")
    @ApiImplicitParam(name = "id", value = "流水号", dataType = "String", paramType = "query", required = false)
    @ApiOperationSupport(order = 20)
    public Result<MapDataVO> getById(@NotEmpty(message = "流水号不能为空") String id) {
        return Result.success(mapDataService.getById(id));
    }

    @PostMapping("add.do")
    @ApiOperation("地图数据表-添加数据")
    @ApiImplicitParams({})
    @ApiOperationSupport(order = 30)
    public Result<Object> add(@Validated(Add.class) MapDataDTO entityDTO) {
        mapDataService.add(entityDTO);
        return Result.success();
    }

    @PostMapping("delete.do")
    @ApiOperation("地图数据表-删除数据")
    @ApiImplicitParam(value = "流水号", name = "id", defaultValue = "1", dataType = "String", paramType = "query", required = true)
    @ApiOperationSupport(order = 40)
    public Result<Object> delete(@ApiIgnore @Validated(Delete.class) MapDataDTO entityDTO) {
        mapDataService.delete(entityDTO);
        return Result.success();
    }

    @PostMapping("update.do")
    @ApiOperation("地图数据表-修改数据")
    @ApiImplicitParams({})
    @ApiOperationSupport(order = 50)
    public Result<Object> update(@Validated(Update.class) MapDataDTO entityDTO) {
        mapDataService.update(entityDTO);
        return Result.success();
    }

    @GetMapping("getIndustryType.do")
    @ApiOperation("地图数据表-获取产业类型")
    @ApiImplicitParams({})
    @ApiOperationSupport(order = 50)
    public Result<Object> getIndustryType() {
        List<MapData> industryTypeList = mapDataService.getIndustryType();
        List<DictDataVO> dictDataVOList = new ArrayList<>();
        industryTypeList.forEach((obj) -> {
            DictDataVO dictDataVO = new DictDataVO();
            dictDataVO.setDictLabel(obj.getCyType());
            dictDataVO.setDictName(obj.getCyType());
            dictDataVO.setDictValue(obj.getCyType());
            dictDataVOList.add(dictDataVO);
        });
        return Result.success(dictDataVOList);
    }

}
